/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import EventEmitter from './eventEmitter3'

let countsum = 20000;

// Test unbinding all event APIs---removeAllListeners
function removeAllListeners_test() {
  let emitterInstance = new EventEmitter();
  let state = '状态信息:\r\n';
  let listener1 = () => {
  };
  let listener2 = () => {
  };
  let callback = (data) => {
    state = state + "\r\n" + `收到事件callback回调,数据为:${data}`;
  }
  emitterInstance.on('listenNum', listener1);
  emitterInstance.on('listenNum', listener2);
  emitterInstance.on('multi', (data) => {
    state = state + "\r\n" + `收到事件multi回调,数据为:${data}`;
  }, '这是multi事件');
  emitterInstance.once('before', (data) => {
    state = state + "\r\n" + `收到事件before回调,数据为:${data}`;
  });
  emitterInstance.addListener('callback', callback);
  const names = emitterInstance.eventNames();
  state =state + "\r\n" + `测试获取所有事件绑定API---eventNames,解除所有绑定的事件之前所有事件名称是:${JSON.stringify(names)}`;
  let start = Date.now();
  for (let i = 0; i < 200000; i++) {
    emitterInstance.removeAllListeners();
  }
  let end = Date.now();
  const count22 = emitterInstance.listeners('listenNum').length;
  const count33 = emitterInstance.listeners('multi').length;
  const count44 = emitterInstance.listeners('before').length;
  const count55 = emitterInstance.listeners('callback').length;
  let total1 = count22 + count33 + count44 
  state =state + "\r\n" + `测试解除所有事件绑定API---removeAllListeners,解除所有绑定的事件之后事件个数${total1}`;
  if (total1 !== 0) {
    throw new Error('eventEmitter3_removeAllListeners err')
  }
  // print(state)
  print(`eventEmitter3_removeAllListeners: ${end - start} ms`);
}

removeAllListeners_test()